package com.jinmdz.fmis.common.util.third;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * 参照C#平台，加密、解密帮助类，DotNet.Kernel.DESEncrypt
 *
 * @author LiCongLu
 * @date 2019-12-30 09:55
 */
public class DesUtil {

    private static final String UTF8 = "UTF-8";
    private static final String DES = "DES";
    private static final String CIPHER = "DES/CBC/PKCS5Padding";


    /**
     * 加密
     *
     * @param text
     * @param secretKey
     * @return
     */
    public static String encrypt(String text, String secretKey) {
        try {
            // 必须是8位，否则会异常
            String md5Key = Md5Util.md5(secretKey, Md5Util.code8);
            byte[] desKey = md5Key.getBytes(UTF8);
            byte[] desIV = md5Key.getBytes(UTF8);

            // 设置密钥参数
            DESKeySpec keySpec = new DESKeySpec(desKey);
            // 获得密钥工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            // 得到密钥对象
            Key key = keyFactory.generateSecret(keySpec);
            // 设置向量
            AlgorithmParameterSpec iv = new IvParameterSpec(desIV);
            Cipher enCipher = Cipher.getInstance(CIPHER);
            // 设置工作模式为加密模式，给出密钥和向量
            enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] pasByte = enCipher.doFinal(text.getBytes(UTF8));
            String format = "%0" + Md5Util.charLen + "X";
            StringBuilder ret = new StringBuilder();
            for (byte b : pasByte) {
                ret.append(String.format(format, b));
            }
            return ret.toString();
        } catch (Exception e) {
            return text;
        }
    }

    /**
     * 解密
     *
     * @param text
     * @param secretKey
     * @return
     */
    public static String decrypt(String text, String secretKey) {
        try {

            int charLen = Md5Util.charLen;
            int len = text.length() / charLen;
            byte[] inputByteArray = new byte[len];
            int x = 0;
            for (int i = 0; i < len; i++) {
                int n = Integer.parseInt(text.substring(i * charLen, i * charLen + charLen), Md5Util.code16);
                inputByteArray[x++] = (byte) (n & 0xff);
            }

            // 必须是8位，否则会异常
            String md5Key = Md5Util.md5(secretKey, Md5Util.code8);
            byte[] desKey = md5Key.getBytes(UTF8);
            byte[] desIV = md5Key.getBytes(UTF8);

            // 设置密钥参数
            DESKeySpec keySpec = new DESKeySpec(desKey);
            // 获得密钥工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
            // 得到密钥对象
            Key key = keyFactory.generateSecret(keySpec);
            // 设置向量
            AlgorithmParameterSpec iv = new IvParameterSpec(desIV);

            Cipher deCipher = Cipher.getInstance(CIPHER);
            deCipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] pasByte = deCipher.doFinal(inputByteArray);

            return new String(pasByte, UTF8);

        } catch (Exception e) {
            e.printStackTrace();
            return text;
        }
    }

    public static void main(String[] args) {
        try {
            String password = "0000";
            String secretKey = "9A89F511BC0C66F1";
            System.out.println("result:" + encrypt(password, secretKey));
            System.out.println("message:" + decrypt(encrypt(password, secretKey), secretKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
